<template>
  <el-dialog
    :model-value="dialogFormVisible"
    :title="title"
    align-center
    draggable
    width="42%"
    style="border-radius: 10px; padding: 5px 10px"
  >
    <el-form :model="form" label-width="120px">
      <el-form-item label="条目名称">
        <el-input v-model="form.name" style="width: 450px" />
      </el-form-item>
      <el-form-item label="计件条目">
        <el-col :span="11">
          <el-input
            v-model="form.target1"
            style="width: 200px"
            placeholder="请填写计件条目一名称"
            prop="inTarget1"
          />
        </el-col>
        <el-col :span="2" class="text-center">
          <span class="text-gray-500">X</span>
        </el-col>
        <el-col :span="11">
          <el-input
            v-model="form.target2"
            style="width: 200px"
            placeholder="请填写计件条目二名称"
          />
        </el-col>
      </el-form-item>
      <el-form-item label="条目一单价">
        <el-input
          v-model="form.targetNum"
          style="width: 200px"
          placeholder="请填写单价"
        />
      </el-form-item>  
      <el-form-item label="是否启用">
        <el-switch v-model="form.state" />
      </el-form-item>
      <el-form-item label="备注说明" class="ins">
        <el-input v-model="form.ins" type="textarea" style="width: 450px" class="ins"/>
      </el-form-item>
      <el-form-item>
        
        <el-button style="width: 180px" type="primary" @click="submitForm"
          >确定</el-button
        >
        <el-button style="width: 180px" @click="onClose">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup>
import { ref, onMounted, reactive, defineProps, defineEmits, watch } from "vue";
import { addRule, updatetRule } from "@/api/piece";
import { getFourthCeoList, addPieceRule } from "@/api/api";
import { ElMessage } from "element-plus";

const form = reactive({
  id: "",
  name: "",
  target1: "",
  targetNum: "",
  target2: "",
  state: true,
  ins: "",
});

const dialogFormVisible = ref(true);
const props = defineProps({
  dialogFormVisible: {
    type: Boolean,
    required: true,
  },
  close: {
    type: Function,
    default: () => {},
  },
  update: {
    type: Function,
    default: () => {},
  },
  selectContent: {
    type: Object,
    required: true,
  },
  type: {
    type: String,
    required: true,
  },
  title: {
    type: String,
    required: true,
  },
});
watch(props, (newValue, oldValue) => {
  if (newValue.selectContent != undefined) {
    form.id = newValue.selectContent.id;
    form.name=newValue.selectContent.name;
    form.target1 = newValue.selectContent.target1;
    form.targetNum = newValue.selectContent.targetNum;
    form.target2 = newValue.selectContent.target2;
    form.state = newValue.selectContent.state == 1 ? true : false;
    form.ins = newValue.selectContent.ins;
  }
});

const onClose = () => {
  props.close();
};

const submitForm = () => {
  if (props.type == "add") {
    addRule(form).then((data) => {
      if (data.code == 200) {
        ElMessage({
          message: "新增成功",
          type: "success",
        });
        props.close();
        props.update();
      } 
      else {
        ElMessage.error("新增失败");
      }
    });
  } 
  else if (props.type == "edit") {
    updatetRule(form).then((data) => {
      if (data.code == 200) {
        ElMessage({
          message: "编辑成功",
          type: "success",
        });
        props.close();
        props.update();
      } 
      else {
        ElMessage.error("编辑失败");
      }
    });
  }
};
</script>
<style lang="scss" scoped >
::v-deep .el-form-item__label:before {
  content: "* ";
  color: red;
}
.ins {
  ::v-deep .el-form-item__label:before {
    content: " ";
  }
}
</style>